package com.superjson.showpro.controller;

import com.alibaba.fastjson.JSONObject;
import com.beantools.filters.BuilderFilters;
import com.beantools.util.ObjectUtil;
import com.superjson.showpro.controller.core.BaseContoller;
import com.superjson.showpro.entity.User;
import com.superjson.showpro.service.SystemRecordService;
import com.superjson.showpro.shared.enums.SignInTypeEnum;
import com.superjson.showpro.shared.exception.GeneralBizException;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.beantools.util.ObjectUtil.isNotNull;
import static com.beantools.util.ObjectUtil.isNull;
import static com.superjson.showpro.constants.SessionConstant.SESSION_USER;
import static com.superjson.showpro.shared.enums.SignInTypeEnum.SIGN_IN_TYPE_ENTERPRISE;

/**
 * Created By zby on 19:59 2019/5/22
 */
@RestController("systemRecordController")
@RequestMapping("/system/Record")
public class SystemRecordController extends BaseContoller {

    @Autowired
    private SystemRecordService systemRecordService;

    /**
     * Created By zby on 17:44 2019/6/10
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Result getSystem(HttpServletRequest request) {
        User user = getUser(request).getResultData();
        String ua = request.getHeader("User-Agent");
        systemRecordService.saveSystem(user, ua, request);
//        总浏览量
        Long todayPageView = systemRecordService.countTodayPageView().getResultData();
//        当日浏览量
        Long allPageView = systemRecordService.countAllPageView().getResultData();
        JSONObject body = new JSONObject();
        if (isNotNull(user)) {
            body.put("id", user.getId());
            SignInTypeEnum signInType = user.getSignInType();
            body.put("name", isNull(signInType) ? null :
                    (signInType.equals(SIGN_IN_TYPE_ENTERPRISE) ? user.getCompanyName() : user.getPhone())
            );
        }
        body.put("todayPageView", todayPageView);
        body.put("allPageView", allPageView);
        return ResultUtil.buildSuccess(body);
    }
}
